<?php

use console\db\Migration;

/**
 * 用户相关信息表
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m150104_091350_t_user extends Migration {

    /**
     * 用户帐号信息表
     * @table `user`
     */
    public function buildTableUser() {
        return [
            'comment' => '用户帐号信息表',
            'columns' => [
                'id' => $this->primaryKey()->comment('用户ID'),
                'username' => $this->string(32)->notNull()->unique()->comment('帐号名'),
                'nickname' => $this->string(32)->comment('昵称'),
                'mobile' => $this->char(32)->unique()->comment('手机号'),
                'email' => $this->char(50)->unique()->comment('邮箱'),
                'avatar' => $this->char(255)->defaultValue('')->comment('头像'),
                'gender' => $this->tinyint(1)->notNull()->unsigned()->defaultValue(0)->comment('性别:0未知，1男，2女'),
                'mobile_hash' => $this->char(255)->comment('加密手机号'),
                'auth_key' => $this->string(32)->notNull()->comment('加密密钥'),
                'password_hash' => $this->char(255)->comment('HASH密码'),
                'password_reset_token' => $this->char(255)->unique()->comment('重置密码时效Token'),
                'ip' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('注册IP'),
                'realname' => $this->string(32)->comment('姓名'),
                'company_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('隶属单位ID'),
                'admin_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('管理员ID'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('注册时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('注销时间'),
            ],
        ];
    }

    /**
     * 用户信息表
     * @table `user_meta`.
     */
    public function buildTableUserMeta() {
        return [
            'comment' => '用户信息表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'cover' => $this->char(255)->defaultValue('')->comment('封面'),
                'signature' => $this->string(128)->defaultValue('')->comment('个人签名'),
                'birthday' => $this->date()->comment('出生日期'),
                'location' => $this->string(64)->defaultValue('')->comment('所在地'),
                'created_at' => $this->unixTimestamp()->comment('注册时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'user_id',
                ]
            ]
        ];
    }

    /**
     * 用户第三方帐号授权表
     * @table `user_connect`.
     */
    public function buildTableUserConnect() {
        return [
            'comment' => '用户第三方登录授权表',
            'columns' => [
                'id' => $this->primaryKey(),
                'client' => $this->char(32)->comment('客户端'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'openid' => $this->char(64)->comment('第三方帐号OPENID'),
                'unionid' => $this->char(64)->comment('第三方帐号UNIONID'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => ['client', 'user_id'],
                    'unique' => true,
                ]
            ]
        ];
    }

    /**
     * 用户IM连接表
     * @table `user_im`.
     */
    public function buildTableUserIm() {
        return [
            'comment' => '用户IM连接表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'im_token' => $this->char(255)->notNull()->comment('IM Token'),
                'expired_at' => $this->unixTimestamp()->comment('过期时间'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'user_id',
                    'unique' => true,
                ]
            ]
        ];
    }

}
